<template>
  <div
    v-if="collect.recipe"
    class="favoritesCookbook"
    @click="toCookbook(collect.recipe.user.user_id)"
  >
    <div
      class="left"
      :style="{
        backgroundImage: `url(${collect.recipe.thumb_path})`,
      }"
    ></div>
    <div class="right">
      <div class="title">{{ collect.recipe.title }}</div>
      <div class="views_count">
        {{ collect.recipe.views_count_text }}
      </div>
      <div class="author">
        <div class="avatar">
          <van-image
            round
            width="18px"
            height="18px"
            :src="collect.recipe.user.user_photo"
            lazy-load
          />
        </div>
        <div class="nickname">{{ collect.recipe.user.nickname }}</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import axios from "axios";
import { onMounted, ref, type Ref } from "vue";
import router from "@/router";

const props = defineProps(["item"]);

const collect: Ref<object> = ref({});

onMounted(async () => {
  // console.log(props.item);

  let res = await axios.get(
    `https://apis.netstart.cn/douguo/recipe/detail/${props.item}`
  );
  // console.log(res.data.result.recipe);
  collect.value = res.data.result;
});

const toCookbook = (user_id: string) => {
  console.log("toCookbook", props.item);
  router.push({
    path: "/cookbook",
    query: { id: props.item, user_id },
  });
};
</script>

<style lang="scss" scoped>
.favoritesCookbook {
  margin-bottom: 15px;
  display: flex;
  cursor: pointer;

  .left {
    height: 122px;
    width: 174px;
    flex-shrink: 0;
    border-radius: 10px;
    background-position: center center;
    background-repeat: no-repeat;
    background-size: 120%;
  }

  .right {
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin-left: 10px;

    .title {
      font-size: 14px;
      margin-bottom: 8px;
    }

    .views_count {
      font-size: 12px;
      margin-bottom: 15px;
    }

    .author {
      display: flex;
      align-items: center;

      .avatar {
        width: 18px;
        height: 18px;
        margin-right: 5px;
      }

      .nickname {
        line-height: 18px;
        font-size: 14px;
        color: #999;
      }
    }
  }
}
</style>
